/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddUser.java
 *
 * Created on Dec 11, 2011, 8:39:27 PM
 */
package view.manage.admin;

import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JOptionPane;
import main.WinkelApplication;
import model.User;

/**
 *
 * @author TommySprat
 */
public class AddUser extends javax.swing.JPanel {

    /** Creates new form AddUser */
    public AddUser() {
        initComponents();
        txNotesField.setText("");
    }

    String getRandomString(int stringLength) {
        String returnString = "";
        SecureRandom random = new SecureRandom();
        int randomPosition;
        String possibleCharacters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        for (int i = 0; i < stringLength; i += 1) {
            randomPosition = random.nextInt(possibleCharacters.length());
            returnString += possibleCharacters.charAt(randomPosition);
        }
        return returnString;
    }

    String getDateAndTime() {
        final String date = "yyyy-MM-dd HH:mm:ss";
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat(date);
        return dateFormat.format(calendar.getTime());
    }

    int getSelectedRol_id() {
        String functie = cbFunctie.getSelectedItem().toString();
        int rol_id = 0;

        if (functie.equalsIgnoreCase("medewerker")) {
            rol_id = 2;
        }
        if (functie.equalsIgnoreCase("manager")) {
            rol_id = 3;
        }
        if (functie.equalsIgnoreCase("Administrator")) {
            rol_id = 4;
        }
        return rol_id;
    }

    String validateInputFields() {
        String validationResult = "approved";

        if (tfGebruikersnaam.getText().isEmpty()) {
            validationResult = "Vul een gebruikersnaam in.";
        }
        if (tfGebruikersnaam.getText().length()>45) {
            validationResult = "Vul een kortere Gebruikersnaam in (Maximaal 45 karakters)";
        }
        if (tfEmail.getText().isEmpty()) {
            validationResult = "Vul het volledige emailadres in.";
        }
        if (tfEmail.getText().length() > 22) {
            validationResult = "Vul een korter emailadres in. (Maximaal 22 karakters)";
        }
        if (tfEmailDomain.getText().isEmpty()) {
            validationResult = "Vul een emaildomein in.";
        }
        if (tfEmailDomain.getText().length() > 20) {
            validationResult = "Vul een korter emaildomein in. (Maximaal 20 karakters)";
        }
        return validationResult;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbGebruikersnaam = new javax.swing.JLabel();
        lbEmail = new javax.swing.JLabel();
        lbFunctie = new javax.swing.JLabel();
        tfGebruikersnaam = new javax.swing.JTextField();
        tfEmail = new javax.swing.JTextField();
        btApply = new javax.swing.JButton();
        lbTitle = new javax.swing.JLabel();
        lbAtmark = new javax.swing.JLabel();
        tfEmailDomain = new javax.swing.JTextField();
        cbFunctie = new javax.swing.JComboBox();
        txNotesField = new javax.swing.JTextArea();
        btBack = new javax.swing.JButton();

        lbGebruikersnaam.setText("Gebruikersnaam");

        lbEmail.setText("email");

        lbFunctie.setText("Functie");

        tfGebruikersnaam.setText("Testuser");

        btApply.setText("Voeg gebruiker toe");
        btApply.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btApplyMouseClicked(evt);
            }
        });

        lbTitle.setFont(new java.awt.Font("Arial", 1, 24));
        lbTitle.setText("Gebruiker toevoegen");

        lbAtmark.setText("@");

        tfEmailDomain.setText("outfit4you.com");

        cbFunctie.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Medewerker", "Manager", "Administrator" }));

        txNotesField.setColumns(20);
        txNotesField.setEditable(false);
        txNotesField.setRows(5);
        txNotesField.setText("(Fout)meldingen komen hier");
        txNotesField.setOpaque(false);

        btBack.setText("Terug");
        btBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btBackActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(lbTitle)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(lbGebruikersnaam)
                            .addGap(88, 88, 88)
                            .addComponent(tfGebruikersnaam))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(lbEmail)
                                .addComponent(lbFunctie)
                                .addComponent(btApply))
                            .addGap(42, 42, 42)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(cbFunctie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(tfEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(lbAtmark)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(tfEmailDomain, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(txNotesField, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE))))
                    .addComponent(btBack))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbTitle)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbGebruikersnaam)
                    .addComponent(tfGebruikersnaam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbEmail)
                    .addComponent(tfEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbAtmark)
                    .addComponent(tfEmailDomain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbFunctie)
                    .addComponent(cbFunctie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btApply)
                    .addComponent(txNotesField, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btBack)
                .addContainerGap(20, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btApplyMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btApplyMouseClicked
        String validationResult = validateInputFields();
        if (validationResult.equalsIgnoreCase("approved")) {
            User newuser = new User();

            newuser.setGebruikersnaam(tfGebruikersnaam.getText());
            newuser.setWachtwoord(getRandomString(8));
            newuser.setEmail(tfEmail.getText() + "@" + tfEmailDomain.getText());
            newuser.setAangemaakt_op(getDateAndTime());
            newuser.setLaatst_ingelogd(getDateAndTime());
            newuser.setRol_id(getSelectedRol_id());
            //System.out.println(getDateAndTime());
            WinkelApplication.getQueryManager().addUser(newuser);

            txNotesField.setText("Gebruiker " + newuser.getGebruikersnaam() + " is aangemaakt\nHet wachtwoord is: " + newuser.getWachtwoord() + "\nGeef dit wachtwoord door.");
        } else {
            JOptionPane.showMessageDialog(WinkelApplication.getInstance().getMainWindow(),validationResult,"Waarschuwing",JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_btApplyMouseClicked

    private void btBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btBackActionPerformed
        WinkelApplication.window.previous();
    }//GEN-LAST:event_btBackActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btApply;
    private javax.swing.JButton btBack;
    private javax.swing.JComboBox cbFunctie;
    private javax.swing.JLabel lbAtmark;
    private javax.swing.JLabel lbEmail;
    private javax.swing.JLabel lbFunctie;
    private javax.swing.JLabel lbGebruikersnaam;
    private javax.swing.JLabel lbTitle;
    private javax.swing.JTextField tfEmail;
    private javax.swing.JTextField tfEmailDomain;
    private javax.swing.JTextField tfGebruikersnaam;
    private javax.swing.JTextArea txNotesField;
    // End of variables declaration//GEN-END:variables
}
